﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LGSVR.Common;
using LGSVR.BLL.Terminal;

namespace LGSVR_WEB.terminal
{
    public partial class CardGroupList : LGSVR.Common.DataBasePage
    {
        private CardGroupBLL groupBLL=new CardGroupBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.parent.navigate('../Logout.aspx');</script>");
            }
            if (!IsPostBack)
            {
                innit();
                gvDataBind("");
            }
           
        }

        private void gvDataBind(string strWere)
        {
            LGSVR.BLL.Sys.CompanyBLL company = new LGSVR.BLL.Sys.CompanyBLL();
            DataTable dt = groupBLL.GetAllGroup(0,"");
          

          

            bool isSupperAdmin = Session["ActName"].ToString() == "超级管理员";
           
            List<string> lstCardGroup = new List<string>(); //有权限的卡组
            if (!isSupperAdmin) //筛选有权限的卡组
            {
                DataTable dtUserCardGroup = GetListByTable("userCardGroup", " state=1 and userID='" + Session["UserID"] + "'");
                lstCardGroup.AddRange(dtUserCardGroup.AsEnumerable().Select(c => c.Field<string>("CardGroupID")));

                dt = (from c in dt.AsEnumerable()
                      where lstCardGroup.Contains(c.Field<string>("groupID"))
                      select c).CopyToDataTable();
            }
         

            dt.Columns.Add("CompanyName");
            foreach (DataRow dr in dt.Rows)
            {
                dr["CompanyName"] = company.GetNameByCompanyID(dr["companyID"].ToString());
            }
            rptBody.DataSource = dt;
            rptBody.DataBind();
            

        }

        private void innit()
        {
            lbtnUnable.Attributes.Add("onclick", "return checkStop('ListInfo')");
            lbtnDel.Attributes.Add("onclick", "return checkDel('ListInfo')");
        }

        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            bool isTrue = Delete("cardGroup", Request["chkId"]);
            if (isTrue)
            {
                MsgContent = ShowMessage("恭喜您，删除成功！", MessageType.Success, false, true);
                NoRefresh();
            }
            else
            { 
                MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
            }
        }

        protected void lbtnUnable_Click(object sender, EventArgs e)
        {
            bool isTrue = UpdateState("cardGroup", false, Request["chkId"]);
            if (isTrue)
            {
                MsgContent = ShowMessage("恭喜您，停用成功！", MessageType.Success, false, true);
                NoRefresh();
            }
            else
            {
                MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
            }
        }
        protected void Search(object sender, CommandEventArgs e)
        {
            string strWhere = "";
            if (txtgroupName.Text.Trim() != "")
            {
                strWhere = " and groupName like '%" + txtgroupName.Text.Trim() + "%'";
            }
            gvDataBind(strWhere);

        }


       
    }
}
